<?php

namespace addons\xapply\controller;

use addons\xapply\Base;
use think\Validate;

class Profile extends Base
{

    protected $noNeedLogin = [];
    protected $noNeedRight = '*';

    /**
     * 元数据
     * url: http://域名/addons/xapply/profile/mine
     * doc: http://lutui.net/doc/app/profile-meta.html
     */
    public function mine()
    {
        $user = $this->auth->getUserinfo();
        return json([
            'info' => [
                'nickname' => $user['nickname'],
                'url' => $user['avatar'],
            ],
            'meta' => [],
            'update' => false,
            'items' => [],
        ]);
    }

    /**
     * 退出登录
     * url: http://域名/addons/xapply/profile/logout
     * doc: http://lutui.net/doc/app/profile-logout.html
     */
    public function logout()
    {
        $this->auth->logout();
        return json(['logout' => 'success']);
    }

    /**
     * 修改密码
     * url: http://域名/addons/xapply/profile/password
     * doc: http://lutui.net/doc/app/profile-password.html
     */
    public function password()
    {
        $password = $this->data['password'];
        if (empty($password)) {
            return json(['error' => '请输入新密码']);
        }
        if (!Validate::make()->check(['newpassword' => $password], ['newpassword' => 'require|regex:\S{6,30}'])) {
            return json(['error' => '密码长度为6~30位']);
        }
        $user = $this->auth->getUserinfo();
        $this->auth->direct($user['id']);
        $ret = $this->auth->changepwd($password, '', true);
        if ($ret) {
            return json(['error' => '']);
        }
        return json(['error' => $this->auth->getError()]);
    }
}